package org.quipu.domain;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "TAG", uniqueConstraints = @UniqueConstraint(columnNames = {"ID_BOOK", "TITLE"}))
@NamedQueries({
    @NamedQuery(name = "Tag.findAll", query = "SELECT t FROM Tag t"),
    @NamedQuery(name = "Tag.findById", query = "SELECT t FROM Tag t WHERE t.id=:id"),
    @NamedQuery(name = "Tag.findByNameForBook", query = "SELECT t FROM Tag t WHERE t.title=:title AND t.book.id=:bookId")
})
public class Tag extends UniqueEntity implements Serializable {

    @Column(name = "TITLE", length = 64, nullable = false)
    private String title;

    @JoinColumn(name = "ID_BOOK", referencedColumnName = "ID", nullable = false)
    @ManyToOne
    private Book book;

    @OneToMany(mappedBy = "tag", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Collection<TransactionTagMap> transactionMapping;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public Collection<TransactionTagMap> getTransactionMapping() {
        return transactionMapping;
    }

    public void setTransactionMapping(Collection<TransactionTagMap> transactionMapping) {
        this.transactionMapping = transactionMapping;
    }

    @Override
    public String toString() {
        return "{ id=" + getId() + "; title=" + getTitle() + "; book=" + getBook() + " }";
    }
}
